package com.mjk.manager.workflow.parser.sql.read.impl;

import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeSpringBean;
import com.mjk.manager.workflow.parser.constant.ReadType;
import com.mjk.manager.workflow.parser.sql.model.ChainModel;
import com.mjk.manager.workflow.parser.sql.read.AbstractSqlRead;
import com.mjk.manager.workflow.parser.sql.vo.SQLParserVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * chain 读取
 *
 * @author tangkc
 * @author houxinyu
 * @since 2.11.1
 */
public class ChainRead extends AbstractSqlRead {

    public ChainRead(SQLParserVO config) {
        super(config);
    }
    @Override
    public Map<String/*规则唯一键*/, String/*规则内容*/> read() {
        // 如果不需要读取直接返回
        if (!needRead()) {
            return new HashMap<>();
        }
        Map<String/*规则唯一键*/, String/*规则*/> result = new HashMap<>();
        try {

            BeeDBService beeDBService = BeeSpringBean.getBean(BeeDBService.class);
            BeeQueryWrapper wrapper = new BeeQueryWrapper(new ChainModel());
            //查找所有能用的业务流 如果这里只返回一个 flowExecutor.execute2Resp 中的channid会匹配不到
            wrapper.eq(ChainModel::getEnable, "1");
            List<ChainModel> chains = Objects.requireNonNull(beeDBService).findManyByWrapper(wrapper);
            for (ChainModel chain : chains) {
                result.put(chain.getChainName(), chain.getElData());
            }
            return result;
        } catch (Exception e) {
            BizLogger.error("table 'chain' read error");
            return result;
        }
    }
 

    @Override
    public ReadType type() {
        return ReadType.CHAIN;
    }
}
